let Promise = require('./source/promise-v2');

// Promise是一个类，无需考虑兼容性问题
// 使用Promise会传入一个执行器， 这个方法会立即执行
// promise状态一旦改变 无法更改
// Promise的三个状态 pending fulfilled rejected
let p1 = new Promise((resolve, reject) => {
    console.log('1');
    resolve('da')
    // setTimeout(() => {
    //     resolve('da')
    // }, 1000)
})

// const s = p1.then((res) => {
//     console.log('res1:', res);
//     // throw new Error('123');
//     return s
// })
// s.then((res) => {
//     console.log('res2:', res)
// }, (error) => {
//     console.log('err:>', error)
// })

console.log('2')


let p2 = new Promise((resolve, reject) => {
    resolve('this is p2');
    // reject('a')
})

let p3 = p2.then((res) => {
    console.log(res);
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(new Promise((resolve, reject) => {
                reject(341)
            }))
        }, 1000)
    })
}, (err) => {
    console.log('p1/s error')
})

p3.then((res) => {
    console.log(res);
}, err => {
    console.log('error')
})